package com.cn.longdistancebusstation.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.cn.longdistancebusstation.R;
import com.cn.longdistancebusstation.adapter.MotorCoachNumberAdapter;
import com.cn.longdistancebusstation.base.BaseActivity;
import com.cn.longdistancebusstation.httpHelper.HttpHelper;
import com.cn.longdistancebusstation.httpapi.WebService;
import com.cn.longdistancebusstation.model.BusInfo;
import com.cn.longdistancebusstation.view.MotorCoachNumberListView;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.IOException;
import java.io.StringReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by Administrator on 2018/1/18.
 */

public class MotorCoachNumberActivity extends BaseActivity implements View.OnClickListener,AdapterView.OnItemClickListener{
    private ImageView iv_back;
    private TextView tv_day_before;
    private TextView tv_day_after;
    private MotorCoachNumberListView list;
    private MotorCoachNumberAdapter adapter;
//    private List<TicketInfo> ticket=new ArrayList<>();
    private List<BusInfo> mBusInfolist;
    private List<String> motorcoachnumber;
    private String destination;
    private String time;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_motorcoachnumber);
        getTitleTextView().setText("到达");
        destination=getIntent().getStringExtra("destination");
        time=getIntent().getStringExtra("time");
        initView();
    }

    private void initView() {
        tv_day_before=(TextView)findViewById(R.id.day_before);
        tv_day_before.setOnClickListener(this);
        tv_day_after=(TextView)findViewById(R.id.day_after);
        tv_day_after.setOnClickListener(this);
        list=(MotorCoachNumberListView)findViewById(R.id.lv_motorCoachNumber);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
        getMotorCoachNumbersList();
        mBusInfolist=new ArrayList<>();
        motorcoachnumber=new ArrayList<>();
        for(int i=0;i<motorcoachnumber.size();i++){
            BusInfo info=new BusInfo();
            info.setBusNo(motorcoachnumber.get(i));
            mBusInfolist.add(info);
        }
        adapter=new MotorCoachNumberAdapter(MotorCoachNumberActivity.this,mBusInfolist);
        list.setAdapter(adapter);
        adapter.notifyDataSetChanged();

    }

    private void getMotorCoachNumbersList() {
        Retrofit retrofit = new HttpHelper.WebServiceBuilder().build().getRetrofit();
        WebService webService = retrofit.create(WebService.class);
        Date today = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strToday = dateFormat.format(today);
        Call<String> call = webService.getBusList("1", "2018-02-01", dateFormat.format(today));
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                try
                {
                    parseXMLForBusList(response.body());
                }
                catch(XmlPullParserException e)
                {}
                catch(IOException e)
                {}
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });
    }
    private void parseXMLForBusList(String xmlText) throws XmlPullParserException, IOException {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xmlPullParser = factory.newPullParser();
        xmlPullParser.setInput(new StringReader(xmlText));
        motorcoachnumber.add(xmlText);
        int eventType = xmlPullParser.getEventType();
        BusInfo busInfo = null;
        String strTagName = "";
        String strTagText = "";
        Boolean isShow = false;
        while(eventType != XmlPullParser.END_DOCUMENT)
        {
            if(eventType == XmlPullParser.START_DOCUMENT)
            {}
            else if(eventType == XmlPullParser.START_TAG)
            {
                strTagName = xmlPullParser.getName();
                if(strTagName.equals("MobilportBliidListHaveDiscount"))
                {
                    busInfo = new BusInfo();
                }
            }
            else if(eventType == XmlPullParser.END_TAG)
            {
                strTagName = "";
                if(xmlPullParser.getName().equals("MobilportBliidListHaveDiscount") && isShow)
                {
                    mBusInfolist.add(busInfo);
                }
            }
            else if(eventType == XmlPullParser.TEXT)
            {
                strTagText = xmlPullParser.getText();
                if(strTagName.equals("SendStationName"))
                {
                    busInfo.setStartStationName(strTagText);
                }
                else if(strTagName.equals("PortName"))
                {
                    busInfo.setEndStationName(strTagText);
                }
                else if(strTagName.equals("BusKindName"))
                {
                    busInfo.setBusType(strTagText);
                }
                else if(strTagName.equals("BliidIsCanSellTicket"))
                {
                    if(strTagText.equals("不可售")) isShow = false;
                    else isShow = true;
                }
                else if(strTagName.equals("Bliid"))
                {
                    busInfo.setBusNo(strTagText);
                }
                else if(strTagName.equals("BliidSendTime"))
                {
                    busInfo.setSendTime(strTagText);
                }
                else if(strTagName.equals("BliidCheckWindowID"))
                {
                    busInfo.setCheckWindowID(strTagText);
                }
                else if(strTagName.equals("PortPrice"))
                {
                    busInfo.setTicketPrice(strTagText);
                }
                else if(strTagName.equals("BliidCanSellNum"))
                {
                    busInfo.setLeftTickets(Integer.parseInt(strTagText));
                }
                else if(strTagName.equals("BliidIsFlow"))
                {
                    busInfo.setFlow(strTagText.equals("0")?false:true);
                }
                else if(strTagName.equals("BliidStartNum"))
                {
                    busInfo.setStartSeatNo(strTagText);
                }
            }
            motorcoachnumber.add(String.valueOf(busInfo));
            eventType = xmlPullParser.next();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.day_before:
                day_before();
                break;
            case R.id.day_after:
                day_after();
                break;
        }

    }

    private void day_before() {
    }
    private void day_after(){

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}
